package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.FindBys;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import utils.Log4Test;

/**
 * Created with IntelliJ IDEA.
 * User: admin
 * Date: 22.05.14
 * Time: 20:01
 * To change this template use File | Settings | File Templates.
 */
public class HomePage {
    protected String URL = "http://rozetka.com.ua/";
    protected final By searchString = By.className("header-search-input-text");
    protected final By compareProduct = By.name("tocomparison");
    protected final By compare = By.name("comparison_catalog");
    protected final String productFoundLocator = "//a[contains(text(),'%s')]";
    public WebDriver driver;

    public HomePage (WebDriver driver)
    {
        this.driver = driver;
    }

    public void open()
    {
        driver.get(URL);
    }

    public boolean isOpened()
    {
        return driver.getCurrentUrl().equals(URL);
    }

    public boolean searchProduct(String productName)
    {
        Log4Test.info("searching product"+productName);
        driver.findElement(searchString).clear();
        driver.findElement(searchString).sendKeys(productName);
        driver.findElement(searchString).sendKeys(Keys.RETURN);
        return driver.findElement(By.xpath(String.format(productFoundLocator,productName))).isDisplayed();
    }

    public void addProductToComparison()
    {
        driver.findElement(compareProduct).click();
    }

    public boolean productPresent(String productName)
    {
        WebDriverWait wait = new WebDriverWait(driver,20);
        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(String.format(productFoundLocator,productName))));
        return driver.findElement(By.xpath(String.format(productFoundLocator,productName))).isDisplayed();
    }

    public void compareProducts()
    {
        WebDriverWait wait = new WebDriverWait(driver,30);
        wait.until(ExpectedConditions.elementToBeClickable(compare));
        driver.findElement(compare).click();
        Log4Test.info("run compare");
    }
}
